package com.example.db;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.example.ConditionHandlers.Conditions_Module;
import com.example.db.sensors.SensorFunctionallityDBWriter;
import com.example.db.sensors.SensorPhoneCallDBWriter;
import com.example.db.sensors.SensorPhoneLockDBWriter;
import com.example.db.sensors.SensorPowerDBWriter;
import com.example.db.sensors.SensorRunningAppsDBWriter;
import com.example.db.sensors.SensorScreenDBWriter;
import com.example.db.sensors.SensorTransportStateDBWriter;
import com.example.db.sensors.SensorVolumeDBWriter;
import com.example.db.sensors.SensorWiredHeadsetDBWriter;
import com.example.priorityobserver.Observable;
import com.example.priorityobserver.Observer;
import com.example.singletones.SQLHelper;

public class DBUpdaterHandler extends Observable<Map<String, Object>> implements
		Observer<Map<String, Object>> {

	protected static DBUpdaterHandler instance;
	List<IDBAction> dbSupportedActions;
	Map<String, IDBAction> supportedActionsMap = new HashMap<String, IDBAction>();
	private SQLHelper mSQLHelper;

	private DBUpdaterHandler() {
		this.registerObserver(Conditions_Module.getInstance());
	}

	public static DBUpdaterHandler getInstance() {
		if (instance == null) {
			instance = new DBUpdaterHandler();
			instance.init();
		}
		return instance;
	}

	protected void init() {
		instance.mSQLHelper = SQLHelper.getInstance();
		instance.addSupportedActions();
	}

	protected void addSupportedActions() {
		dbSupportedActions = new Vector<IDBAction>();
		dbSupportedActions.add(new ActionsComplete(mSQLHelper));
		dbSupportedActions.add(new SensorFunctionallityDBWriter(mSQLHelper));
		dbSupportedActions.add(new SensorPhoneCallDBWriter(mSQLHelper));
		dbSupportedActions.add(new SensorPhoneLockDBWriter(mSQLHelper));
		dbSupportedActions.add(new SensorPowerDBWriter(mSQLHelper));
		dbSupportedActions.add(new SensorRunningAppsDBWriter(mSQLHelper));
		dbSupportedActions.add(new SensorScreenDBWriter(mSQLHelper));
		dbSupportedActions.add(new SensorTransportStateDBWriter(mSQLHelper));
		dbSupportedActions.add(new SensorVolumeDBWriter(mSQLHelper));
		dbSupportedActions.add(new SensorWiredHeadsetDBWriter(mSQLHelper));
		for (IDBAction i : dbSupportedActions)
			supportedActionsMap.put(i.getName(), i);
	}

	public String assignParameters(String query, List<String> parameters) {
		String returnVal = new String(query);
		for (String j : parameters) {
			String updaterName = j.substring(1, j.indexOf("|"));
			IDBAction action = supportedActionsMap.get(updaterName);
			if (action != null) {
				returnVal = action.updateParameter(j, returnVal);
			}
		}
		return returnVal;
	}

	public void update(Observable<Map<String, Object>> observable,
			Map<String, Object> data) {
		boolean isApplicable = false;
		for (IDBAction i : dbSupportedActions)
			if (i.isApplicable(observable, data)){
				i.updateDB(observable, data);
				isApplicable = true;
			}
		if ( isApplicable )
			notifyObservers(data);
	}
}
